<?php

function str_replace_once($needle, $replace, $haystack)
{
// Looks for the first occurence of $needle in $haystack
// and replaces it with $replace.
    $pos = strpos($haystack, $needle);
    if ($pos === false)
    {
        // Nothing found
        return $haystack;
    }
    return substr_replace($haystack, $replace, $pos, strlen($needle));
}

function genLink()
{
    $today = date('md');
    $filename = 'malllink/' . $today . '.txt';
    $mall = '';
    if (!file_exists($filename))
    {
        $mall = file_get_contents('http://xiazaiquan.com/mall/forum.html');
        $mall = trim($mall);
        if ($mall != '')
        {
            file_put_contents($filename, $mall);
        }
        else
        {
            $list = scandir('malllink');
            $list = array_diff($list, array('.', '..'));
            $file = 'malllink/' . $list[array_rand($list)];
            $mall = file_get_contents($file);
        }
    }
    else
    {
        $list = scandir('malllink');
        $list = array_diff($list, array('.', '..'));
        $file = 'malllink/' . $list[array_rand($list)];
        $mall = file_get_contents($file);
    }
    return $mall;
}

include_once('Smarty.class.php');
$main_smarty = new Smarty;
include('config.php');
include(mnminclude . 'html1.php');
include(mnminclude . 'link.php');
include(mnminclude . 'tags.php');
include(mnminclude . 'user.php');
include(mnminclude . 'smartyvariables.php');

$Story_Content_Tags_To_Allow = Story_Content_Tags_To_Allow_God;

$skipthis = 0;
$linkres = new Link;
$linkres->randkey = rand(10000, 10000000);
$linkres->status = 'published';
$linkres->author = empty($_POST['author']) ? 1 : $_POST['author'];
$linkres->title = trim($_POST['title']);
$linkres->title = strip_tags($linkres->title);
$linkres->title_url = makeUrlFriendly($linkres->title);
$linkres->url = $_POST['url'];
$linkres->url = str_replace('caiji','view',$linkres->url);
$linkres->url_title = $linkres->title;
$linkres->content = $_POST['content'];
$linkres->content = strip_tags($linkres->content, $Story_Content_Tags_To_Allow);

$malllinks = genLink();
$malllinks = trim($malllinks);
if ($malllinks != '')
{    
    $mall = str_replace(
                    array(
                        '<h2>商城热卖</h2>',
                        '<ul>',
                        '</ul>',
                        '<li>'
                    ),
                    '', $malllinks);
    $mall = trim($mall);
    $mall = explode('</li>', $mall);
    $mall = array_diff($mall, array(''));
    $mallnum = array_rand($mall);
    $mall = $mall[$mallnum];
    $mall = str_replace('href="', 'class="none" href="', $mall);
    if(strpos($mall, 'xiazaiquan') == false)
    {
        $mall = str_replace('/mall/', 'http://xiazaiquan.com/mall/', $mall);
    }
    $malllinks='<div id="mall">'.$malllinks.'</div>';
    $linkres->content = str_replace_once('</a>', '</a>' . $mall, $linkres->content);
    $linkres->content.=$malllinks;
}

$linkres->category = empty($_POST['category']) ? 12 : $_POST['category'];
$linkres->link_summary = close_tags(utf8_substr(strip_tags($linkres->content), 0, StorySummary_ContentTruncate - 1));

if ($linkres->url != "" && $skipthis == 0)
{  // 0 means don't allow, 1 means allow
    if ($linkres->duplicates($linkres->url) > 0)
    {
        $skipthis = 1;
    }
}

if ($skipthis == 0)
{
    $linkres->store();
    //totals_adjust_count($linkres->status, 1);
    //tags_insert_string($linkres->id, $dblang, $linkres->tags);
    //$linkres->store_basic();
    echo "发布成功";
}
else
{
    echo "发布失败";
}
?>